<?php
namespace app\api\controller;

use app\api\model\ExchangePrize;
use app\api\model\ExchangeRecord;
use think\Request;


//兑换奖品
class Exchange extends Common
{
    //获取 兑换奖品列表
    public function getPrizeList(Request $request)
    {
        $user_id = $this->checkLoginUser(true);
        to_json_encode(200,'success',ExchangePrize::handlePrizeList(ExchangePrize::getPrizeList(),$user_id));
    }
    public function getPrizeInfo(Request $request)
    {
        $user_id = $this->checkLoginUser(true);

        $prize_id = $request->param('prize_id',0,'intval');

        if(empty($prize_id))
        {
            to_json_encode(400,'参数错误',['status'=>false]);
        }
        
        to_json_encode(200,'success',ExchangePrize::handlePrizeInfo(ExchangePrize::getPrizeInfo($prize_id),$user_id));
        
    }
    //兑换 奖品
    public function postExchangePrize(Request $request)
    {
        $user_id = $this->checkLoginUser(true);

       
        $prize_id = $request->post('prize_id',0,'intval');
       
        if(empty($prize_id))
        {
            to_json_encode(400,'参数错误',['status'=>false]);

        }

        $prize_info = ExchangePrize::getPrizeInfo($prize_id);
        if(empty($prize_info))
        {
            to_json_encode(400,'没有该兑换奖品',['status'=>false]);
        }

        $res = ExchangeRecord::handleExchangePrize($user_id,$prize_id,$prize_info['prize_points']);

        if(!$res['status'])
        {
            to_json_encode(400,$res['msg'],['status'=>false]);

        }

        to_json_encode(200,'success',['status'=>true]);

    }

}
